﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using Microsoft.SharePoint;

namespace DR5.SPExtend
{
    class CsvUtility
    {
        public static DataTable CSVToDataTable(string filePath)
        {
            string line = string.Empty;
            try
            {
                StreamReader sr = new StreamReader(filePath);
                line = sr.ReadLine();
                line = sr.ReadLine();

                string[] value = line.Split(',');
                DataTable dt = new DataTable();
                DataRow row;
                foreach (string dc in value)
                {
                    dt.Columns.Add(new DataColumn(dc));
                }

                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine();

                    if (line.Contains("\""))
                    {
                        //Comma(,) is used in text
                        int startQuoteIndex = line.IndexOf("\"");
                        int endQuoteIndex = line.IndexOf("\"", startQuoteIndex + 1);

                        do
                        {
                            int commaIndex = line.IndexOf(',', startQuoteIndex, endQuoteIndex - startQuoteIndex);
                            line = line.Remove(commaIndex, 1);
                        }
                        while (line.IndexOf(',', startQuoteIndex, endQuoteIndex - startQuoteIndex) != -1);

                        //line.Remove(startQuoteIndex, endQuoteIndex - startQuoteIndex);
                    }

                    value = line.Split(',');

                    if (value.Length == dt.Columns.Count)
                    {
                        row = dt.NewRow();
                        row.ItemArray = value;
                        dt.Rows.Add(row);
                    }
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception("Exception at record '" + line + "'. <br /><b>Please rectify the record.</b>");
            }
        }

        public static DataTable CSVToDataTable(SPFile file)
        {
            string line = string.Empty;
            try
            {
                //Header Creation Start Here
                StreamReader sr = new StreamReader(file.OpenBinaryStream());
                line = sr.ReadLine();

                string[] value = line.Split(',');
                DataTable dt = new DataTable();
                DataRow row;
                foreach (string dc in value)
                {
                    dt.Columns.Add(new DataColumn(dc));
                }
                //Header Creation End Here

                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine();

                    if (line.Contains("\""))
                    {
                        //Comma(,) is used in text
                        int startQuoteIndex = line.IndexOf("\"");
                        int endQuoteIndex = line.IndexOf("\"", startQuoteIndex + 1);

                        do
                        {
                            int commaIndex = line.IndexOf(',', startQuoteIndex, endQuoteIndex - startQuoteIndex);
                            line = line.Remove(commaIndex, 1);
                        }
                        while (line.IndexOf(',', startQuoteIndex, endQuoteIndex - startQuoteIndex) != -1);

                        //line.Remove(startQuoteIndex, endQuoteIndex - startQuoteIndex);
                    }

                    value = line.Split(',');

                    if (value.Length == dt.Columns.Count)
                    {
                        row = dt.NewRow();
                        row.ItemArray = value;
                        dt.Rows.Add(row);
                    }
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception("Exception at record '" + line + "'. <br /><b>Please rectify the record.</b>");
            }
        }
    }
}
